<?php

namespace App\Http\Controllers\Admin;

use App\Http\Query\CategoryQuery;
use app\Model\Base;
use app\Model\OrderModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Log;
use function MongoDB\BSON\toJSON;
use SplFileObject;

class JapanRetainController extends Controller

{

    function retain_list(Request $request)
    {
        $getParam = $request->all();
        $map = [];
        if (!empty($getParam['corp_id'])) {
            $map['cmm.corp_id'] = $getParam['corp_id'];
        }
//        if (!empty($getParam['deliver_state'])) {
//            dump($getParam['deliver_state']);
//            $map['uod.deliver_state'] = '0';
//        }
//        dump($getParam);

        if (!empty($getParam['deliver_state'])) {
//            dump($getParam['deliver_state']);
            $map['uod.deliver_state'] = '0';
        }
        $db = DB::table('user_order as uo')
            ->select(DB::raw('smm.shop_name,uod.deliver_state,uod.courier_number,uo.create_time,uod.old_detail_id,uo.order_id,cm.category_name,gm.goods_name,gm.goods_id,uod.color,uod.size,uod.goods_num,uod.deliver_time,uod.company_name,uod.delivery_time_finish,uo.add,uod.courier_number'))
            ->leftJoin('user_order_detailed as uod', 'uo.order_id', '=', 'uod.order_id')
            ->leftJoin('goods_master as gm', 'uod.goods_id', '=', 'gm.goods_id')
            ->leftJoin('goods_model as gmm', 'gm.goods_id', '=',  DB::raw('gmm.goods_id and gmm.goods_model_id = uod.model_id'))
            ->leftJoin('category_master as cm', 'gm.category_id', '=', 'cm.category_id')
            ->leftJoin('user_master as um', 'uo.user_id', '=', 'um.user_id')
            ->leftJoin('corp_master as cmm', 'cmm.corp_id', '=', 'um.corp_id')
            ->leftJoin('shop_master as smm', 'smm.shop_id', '=', 'uo.shop_id')
            ->whereNotNull('uod.old_detail_id')
            ->where($map)
            ->orderBy('uo.create_time', 'asc');
        if (!empty($getParam['star_date']) && !empty($getParam['end_date'])) {
            $list = $db->whereBetween('uo.create_time', [$getParam['star_date'], $getParam['end_date']])->get();
        } else {
            $list = $db->get();
        }
        foreach ($list as $key => $value) {
//            dump($value->old_detail_id);
            $map['detail_id'] = $value->old_detail_id;
            $temp = DB::table('user_order_detailed')
                ->where($map)
                ->first();
            $list[$key]->old_size = $temp->size;
        }
//        dump($list);
//        dump($getParam);
        $mo = new \App\Model\OrderModel();
        return view('warehouse.retain', array(
            'list' => $list,
            'companys' => $mo::companys(),
            'param' => $getParam,
        )); // 返回视图
    }
}
